package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;

import com.example.myapplication.fragment.HelpFragment;
import com.example.myapplication.fragment.HomeFragment;
import com.example.myapplication.fragment.NewsFragment;
import com.example.myapplication.fragment.PersonFragment;
import com.example.myapplication.fragment.ServletFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {
    private HelpFragment helpFragment;
    private HomeFragment homeFragment;
    private ServletFragment servletFragment;
    private NewsFragment newsFragment;
    private PersonFragment personFragment;
    private BottomNavigationView bottom;

    private BotviewReceiver botviewReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bottom = findViewById(R.id.bottom);
        helpFragment = new HelpFragment();
        homeFragment = new HomeFragment();
        servletFragment = new ServletFragment();
        newsFragment = new NewsFragment();
        personFragment = new PersonFragment();


        //动态注册
        botviewReceiver = new BotviewReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("servlet");
        registerReceiver(botviewReceiver, filter);

        getSupportFragmentManager().beginTransaction().replace(R.id.main,homeFragment).commit();
        bottom.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch(item.getItemId()){
                    case R.id.home:
                        getSupportFragmentManager().beginTransaction().replace(R.id.main,homeFragment).commit();
                        break;
                    case R.id.servlet:
                        getSupportFragmentManager().beginTransaction().replace(R.id.main,servletFragment).commit();
                        break;
                    case R.id.help:
                        getSupportFragmentManager().beginTransaction().replace(R.id.main,helpFragment).commit();
                        break;
                    case R.id.news:
                        getSupportFragmentManager().beginTransaction().replace(R.id.main,newsFragment).commit();
                        break;
                    case R.id.person:
                        getSupportFragmentManager().beginTransaction().replace(R.id.main,personFragment).commit();
                        break;
                }
                return true;
            }
        });


    }
    //第一步，写一个广播接收器
    //2:动态注册
    //第三步，发送广播
    public class BotviewReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if("servlet".equals(action)){
                bottom.setSelectedItemId(R.id.servlet);
            }

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(botviewReceiver);
    }

}